<template>
    <div class="edit">
        <el-form :model="form" :rules="rules" label-width="80px" ref="form">
            <el-form-item label="课程名" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="授课教师" prop="teacherId">
                <el-select filterable v-model="form.teacherId" placeholder="请选择教师">
                    <el-option v-for="item in options" :key="item.value" :label="item.name"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="年级" prop="grade">
                <el-input-number v-model.number="form.grade" controls-position="right"></el-input-number>
            </el-form-item>
            <el-form-item label="上课时间">
                <el-select filterable v-model="dayId">
                    <el-option v-for="item in days" :key="item.value" :label="item.day" :value="item.id"></el-option>
                </el-select>
                <el-select filterable v-model="lessonId">
                    <el-option v-for="item in lessons" :key="item.value" :label="item.lesson"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="时长(节)">
                <el-input-number v-model.number="lessonNumber" type="number"
                    controls-position="right"></el-input-number>
            </el-form-item>
            <el-form-item label="上课地点" prop="location">
                <el-input v-model="form.location"></el-input>
            </el-form-item>
            <el-form-item label="学分" prop="credit">
                <el-input-number v-model.number="form.credit" type="number" controls-position="right"></el-input-number>
            </el-form-item>
            <el-form-item label="最大容量" prop="maxSize">
                <el-input-number v-model.number="form.maxSize" type="number"
                    controls-position="right"></el-input-number>
            </el-form-item>
            <el-form-item label="考试地点" prop="examLocation">
                <el-input v-model="form.examLocation"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {
    teacherAll,
    courseAdd,
    courseEdit,
    getCourse,
} from "@/api/course.js"
export default {
    props: ['type', 'id'],
    data() {
        return {
            options: [],
            form: {
                id: -1,
                name: '',
                teacherId: '',
                grade: 2019,
                time: '2-2-2',
                location: '',
                credit: 2,
                maxSize: 50,
                examLocation: '',
            },
            rules: {
                name: [
                    { require: true, message: '请输入课程名', trigger: 'blur' },
                ],
                teacherId: [
                    { require: true, type: 'number', message: '请选择授课教师', trigger: 'blur' },
                ],
                grade: [
                    { require: true, type: 'number', message: '请输入年级', trigger: 'blur' },
                ],
                location: [
                    { require: true, message: '请输入上课地点', trigger: 'blur' },
                ],
                credit: [
                    { require: true, type: 'number', message: '请输入学分', trigger: 'blur' },
                ],
                maxSize: [
                    { require: true, type: 'number', message: '请输入最大容量', trigger: 'blur' },
                ],
                examLocation: [
                    { require: true, message: '请输入考试地点', trigger: 'blur' },
                ],
            },
            days: [
                {
                    id: 1,
                    day: '星期一',
                },
                {
                    id: 2,
                    day: '星期二',
                },
                {
                    id: 3,
                    day: '星期三',
                },
                {
                    id: 4,
                    day: '星期四',
                },
                {
                    id: 5,
                    day: '星期五',
                },
                {
                    id: 6,
                    day: '星期六',
                },
                {
                    id: 7,
                    day: '星期日',
                },
            ],
            lessons: [
                {
                    id: 1,
                    lesson: '第一节',
                },
                {
                    id: 2,
                    lesson: '第二节',
                },
                {
                    id: 3,
                    lesson: '第三节',
                },
                {
                    id: 4,
                    lesson: '第四节',
                },
                {
                    id: 5,
                    lesson: '第五节',
                },
                {
                    id: 6,
                    lesson: '第六节',
                },
                {
                    id: 7,
                    lesson: '第七节',
                },
                {
                    id: 8,
                    lesson: '第八节',
                },
                {
                    id: 9,
                    lesson: '第九节',
                },
            ],
            dayId: 2,
            lessonId: 2,
            lessonNumber: 2,
        }
    },
    methods: {
        async getOptions() {
            let ret = await teacherAll()
            this.options = ret.data
        },
        async getName(id) {
            if (this.type === 'edit') {
                let ret = await getCourse(id);
                this.form = ret.data
                this.dayId = ret.data.time.split('-')[0]
                this.lessonId = ret.data.time.split('-')[1]
                this.lessonNumber = ret.data.time.split('-')[2]
            }
        },
        submit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    this.form.time = [this.dayId, this.lessonId, this.lessonNumber].join('-')
                    if (this.type === 'add') {
                        courseAdd(this.form).then(res => {
                            if (res.code === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '添加成功',
                                    duration: 1000,
                                });
                                this.$emit('done')
                            }
                        })
                    } else if (this.type === 'edit') {
                        courseEdit(this.form).then(res => {
                            if (res.code === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '修改成功',
                                    duration: 1000,
                                });
                                this.$emit('done')
                            }
                        })
                    }
                }
            });
        },
        cancel() {
            this.$emit('cancel')
        },
    },
    watch: {
        id: {
            handler(newVal, oldVal) {
                this.getName(newVal)
            },
            immediate: true
        }
    },
    mounted() {
        this.getOptions()
    }
}
</script>